<?php
if(!function_exists('editImageUrl')){
    function editImageUrl($vals){
        foreach ($vals as $val){
            if($val->coverimage){
                $val->coverimage = config('domain').'/'.$val->coverimage;
            }
            if($val->lineimage){
                $val->lineimage = config('domain').'/'.$val->lineimage;
            }
            if($val->src){
                $val->src = config('domain').'/'.$val->src;
            }
            if($val->audiourl){
                $val->audiourl = config('domain').'/'.$val->audiourl;
            }
            if($val->songurl){
                $val->songurl = config('domain').'/'.$val->songurl;
            }
            $arr=[];
            if($val->bannerimage!=null){
                foreach ($val->bannerimage as $banner){
                    $banner = config('domain').'/'.$banner;
                    array_push($arr,$banner);
                }
                $val->bannerimage = $arr;
            }
        }
    }
}

if(!function_exists('keywords_check')){
    function keywords_check($content){
        $keywords = explode(',',config('keywords'));
        return str_replace($keywords,"**",$content);
    }
}
if(!function_exists('is_yesterday')){
    function is_yesterday($time=0){
        $today = strtotime(date('Y-m-d',time()));
        $tmp = $today - $time;
        if($tmp > 0 && $tmp <= 3600 * 24){
            return true;
        }else{
            return false;
        }
    }
}
if(!function_exists('is_today')){
    function is_today($time=0){
        $today = strtotime(date('Y-m-d',time()));
        $tmp = $time-$today;
        if($tmp > 0 && $tmp <= 3600 * 24){
            return true;
        }else{
            return false;
        }
    }
}
if(!function_exists('array_equal')){
    function array_equal($array1,$array2){
        if(count($array2)>count($array1)){
            $temp = $array2;
            $array2 = $array1;
            $array1 =$temp;
        };
        if(!array_diff($array1,$array2))
            return true;
        else
            return false;
    }
}
if(!function_exists('get_score')){
    function get_score($fraction,$score){
        if($fraction<60){
            return false;
        }elseif ($fraction>=60&&$fraction<80){
            return ceil($score/2);
        }elseif ($fraction>=80){
            return $score;
        }
    }
}
if(!function_exists('getExpressInfo')){
    function getExpressInfo($vals){
        foreach ($vals as $val){
            if($val->express_id&&$val->ecompany){
                $logisticResult=getOrderTracesByJson($val->ecompany,$val->express_id);
                $val->expressInfo=$logisticResult;
            }
        }
    }
}
function getOrderTracesByJson($ShipperCode,$LogisticCode){
    $requestData= "{'OrderCode':'','ShipperCode':'".$ShipperCode."','LogisticCode':'".$LogisticCode."'}";
    $datas = array(
        'EBusinessID' => '1404745',
        'RequestType' => '1002',
        'RequestData' => urlencode($requestData) ,
        'DataType' => '2',
    );
    $ReqURL='http://api.kdniao.cc/Ebusiness/EbusinessOrderHandle.aspx';
    $AppKey='42163cdb-f7f0-49d2-a8a2-982748bc8222';
    $datas['DataSign'] = my_encrypt($requestData, $AppKey);
    $result=sendPost($ReqURL, $datas);
    return $result;
}
function sendPost($url, $datas) {
    $temps = array();
    foreach ($datas as $key => $value) {
        $temps[] = sprintf('%s=%s', $key, $value);
    }
    $post_data = implode('&', $temps);
    $url_info = parse_url($url);
    if(empty($url_info['port']))
    {
        $url_info['port']=80;
    }
    $httpheader = "POST " . $url_info['path'] . " HTTP/1.0\r\n";
    $httpheader.= "Host:" . $url_info['host'] . "\r\n";
    $httpheader.= "Content-Type:application/x-www-form-urlencoded\r\n";
    $httpheader.= "Content-Length:" . strlen($post_data) . "\r\n";
    $httpheader.= "Connection:close\r\n\r\n";
    $httpheader.= $post_data;
    $fd = fsockopen($url_info['host'], $url_info['port']);
    fwrite($fd, $httpheader);
    $gets = "";
    $headerFlag = true;
    while (!feof($fd)) {
        if (($header = @fgets($fd)) && ($header == "\r\n" || $header == "\n")) {
            break;
        }
    }
    while (!feof($fd)) {
        $gets.= fread($fd, 128);
    }
    fclose($fd);
    return $gets;
}

function my_encrypt($data, $appkey) {
    return urlencode(base64_encode(md5($data.$appkey)));
}
